/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package xmarti.vacaxm.model;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.hibernate.EntityMode;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import xmarti.vacaxm.beans.sequence.*;
import xmarti.vacaxm.exception.ExceptionHibernateVacaxm;
import xmarti.vacaxm.utils.HibernateUtil;

/**
 *
 * @author xmarti
 */
public class ModelSequence extends HibernateUtil {
    
   public SequenceType getById(int id) throws ExceptionHibernateVacaxm{
       return getObjectById(id,SequenceType.class);
   }
    public List<State> getListState() throws ExceptionHibernateVacaxm{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<State> resultList = new ArrayList<State>();
        try
        {            
            resultList= session.createQuery("FROM State c order by c.name" ).list();
        }
        catch (HibernateException he)
        {
            throw new ExceptionHibernateVacaxm(ExceptionHibernateVacaxm.ERR_SELECT);
        }
        return resultList;
    }
    public List<StateDefault> getListStateDefault(SequenceType sequenceType) throws ExceptionHibernateVacaxm{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<StateDefault> resultList = new ArrayList<StateDefault>();
        Set<StateDefault> setList= new HashSet<StateDefault>();
        try
        {            
            resultList=    session.createQuery("FROM StateDefault c WHERE c.sequenceType.id="+sequenceType.getId() + " ORDER BY c.order" ).list();
            for(StateDefault s:resultList){
                setList.add(s);
            }
        }
        catch (HibernateException he)
        {
            throw new ExceptionHibernateVacaxm(ExceptionHibernateVacaxm.ERR_SELECT);
        }
        return resultList;
    }
     public List<SequenceType> getListSequencesType() throws ExceptionHibernateVacaxm{
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List<SequenceType> resultList = new ArrayList<SequenceType>();
        try
        {            
            resultList=  session.createQuery("FROM SequenceType c order by c.name" ).list();
        }        
        catch (HibernateException he)
        {
            throw new ExceptionHibernateVacaxm(ExceptionHibernateVacaxm.ERR_SELECT);
        }
        return resultList;
    }
    public int maxIdStateDefault(SequenceType sequenceType){
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        int id=0;
        try{
            id= (int) session.createQuery("select MAX(s.id) FROM StateDefault s WHERE s.sequenceType.id="+sequenceType.getId()).uniqueResult();
        }catch (NullPointerException e){
            return 0;
        }
        return id;    
    }
    public int deleteStateDefault(StateDefault stateDefault){
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        int result=0;
        String hql= "DELETE FROM StateDefault where id= :id and idSequenceType= :sid ";
        Query query=session.createQuery(hql);
        query.setParameter("id",stateDefault.getId());
        query.setParameter("sid",stateDefault.getSequenceType().getId());
        result= query.executeUpdate();
        return result;
    }
    public SequenceType getSequenceTypeByName(String name){
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        SequenceType sequenceType=null;
        String hql= "FROM SequenceType s WHERE s.name= :id ";
        Query query=session.createQuery(hql);
        query.setParameter("id",name);
        sequenceType= (SequenceType) query.uniqueResult();
        return sequenceType;
    }
    public int add(State object) throws ExceptionHibernateVacaxm {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
                int state=0;                
		try{			                        
                        session.save(object);                                                
			state= object.getId();
                        session.getTransaction().commit();
                        
		}catch (Exception e){			
			session.getTransaction().rollback();		
                        if (e.getCause().toString().contains("name_UNIQUE")){
                            throw new ExceptionHibernateVacaxm(ExceptionHibernateVacaxm.ERR_INSERT_NAME_UNIQUE);
                        }else{
                            throw new ExceptionHibernateVacaxm(ExceptionHibernateVacaxm.ERR_INSERT);			
                        }                        
		}		
		return state;
	}
    
}
